﻿/*----------------------------------------------------------------
//  <copyright file="BatchEntityConfiguration.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-07-10 17:06</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Entity.SqlServer.BatchConfiguration
{
    #region "实体与SqlServer持久化对象映射的常规属性配置"
    /// <summary>
    /// 实体与SqlServer持久化对象映射的常规属性配置
    /// </summary>
    public class BatchEntityConfiguration : IEntityBatchConfiguration
    {
        #region "方法"
        #region "配置指定的<see cref="IMutableEntityType"/>"
        /// <summary>
        /// 配置指定的<see cref="IMutableEntityType"/>
        /// </summary>
        /// <param name="modelBuilder">模型构建器</param>
        /// <param name="mutableEntityType">实体的 <see cref="IMutableEntityType"/> 类型</param>
        /// <param name="database">数据库相关信息和操作的访问 <see cref="DatabaseFacade"/> 类型</param>
        /// <param name="dbContextOptions">数据上下文配置 <see cref="DbContextOption"/> 类型</param>
        /// <param name="dbContextType">数据上下文类型</param>
        public void Configure(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType, DatabaseFacade database, DbContextOption dbContextOptions, Type dbContextType)
        {
            //配置内存优化表
            var isMemoryOptimized = IsMemoryOptimized(mutableEntityType.ClrType);
            if (isMemoryOptimized)
            {
                //modelBuilder.Entity(mutableEntityType.ClrType).IsMemoryOptimized(); //7.0已过时
                modelBuilder.Entity(mutableEntityType.ClrType).ToTable(t => t.IsMemoryOptimized());
            }
        }
        #endregion

        #endregion

        #region "受保护的方法"
        #region "判断实体类型是否启用内存优化"
        /// <summary>
        /// 判断实体类型是否启用内存优化
        /// </summary>
        /// <param name="entityType">实体类型</param>
        /// <returns></returns>
        protected virtual bool IsMemoryOptimized(Type entityType)
        {
            MemoryOptimizedAttribute attribute = entityType.GetAttribute<MemoryOptimizedAttribute>();
            return attribute?.Enabled == true;
        }
        #endregion

        #endregion

    }
    #endregion

}
